<template>
  <div class="dhcc-body">
    <el-row :gutter="10">
      <el-col :span="6">
        <div class="dhcc-search">
          <el-form
            ref="orgSearchForm"
            :model="orgMap"
            class="demo-form-inline"
            size="mini"
            :inline="true"
          >
            <el-form-item prop="orgName">
              <el-input
                v-model="orgMap.orgName"
                placeholder="机构名称"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="fetchData" icon="el-icon-search"
                >查询</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="dhcc-main">
          <!-- 表格数据 -->
          <el-table
            :data="orgList"
            height="550"
            border
            style="width: 100%"
            highlight-current-row
            @current-change="changOrgRows"
            size="mini"
          >
            <el-table-column
              prop="orgKey"
              label="机构代码"
              width="80"
            ></el-table-column>
            <el-table-column prop="orgName" label="机构名称"></el-table-column>
          </el-table>
          <el-pagination
            @size-change="orgHandleSizeChange"
            @current-change="orgHandleCurrentChange"
            :current-page="orgCurrentPage"
            :page-sizes="[10, 20, 50]"
            :page-size="orgPageSize"
            layout="total, sizes, prev, next"
            :total="orgTotal"
            style="margin-top: 10px"
          ></el-pagination>
        </div>
      </el-col>
      <el-col :span="18">
        <div class="dhcc-search">
          <!-- 检索区 -->
          <el-form
            ref="searchForm"
            :model="userMap"
            class="demo-form-inline"
            size="mini"
            :inline="true"
          >
            <el-form-item prop="deptName">
              <el-input
                v-model="userMap.userName"
                placeholder="用户名称"
              ></el-input>
            </el-form-item>
            <el-form-item prop="activeFlg">
              <el-radio-group v-model="userMap.activeFlg" @change="chageStatus">
                <el-radio label="1">启用</el-radio>
                <el-radio label="0">停用</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                @click="selectUserData"
                icon="el-icon-search"
                >查询</el-button
              >
              <el-button type="primary" @click="handleAdd" icon="el-icon-plus"
                >新建</el-button
              >
              <el-button @click="resetForm('searchForm')" icon="el-icon-refresh"
                >重置</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="dhcc-main">
          <!-- 表格数据 -->
          <el-table
            :data="list"
            height="550"
            border
            style="width: 100%"
            size="mini"
          >
            <el-table-column
              type="index"
              label="序号"
              width="60"
            ></el-table-column>
            <el-table-column
              prop="userName"
              label="用户名称"
              width="150"
            ></el-table-column>
            <el-table-column prop="sexName" label="性别" width="80">
            </el-table-column>
            <el-table-column
              prop="loginName"
              label="登录名"
              width="150"
            ></el-table-column>
            <el-table-column label="密码" width="80" align="center">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="danger"
                  @click="handlePassword(scope.row.userKey)"
                  >重置</el-button
                >
              </template>
            </el-table-column>
            <el-table-column prop="introduction" label="简介"></el-table-column>
            <el-table-column
              prop="status"
              label="状态"
              align="center"
              width="80"
            >
              <template slot-scope="scope">
                <span>{{ scope.row.activeFlg | statusFilter }}</span>
              </template>
            </el-table-column>
            <el-table-column label="启用/停用" width="80" align="center">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="warning"
                  @click="
                    handleIsDisplay(scope.row.userKey, scope.row.activeFlg)
                  "
                  >{{ scope.row.activeFlg == 1 ? "停用" : "启用" }}</el-button
                >
              </template>
            </el-table-column>
            <el-table-column label="操作" width="80" align="center">
              <template slot-scope="scope">
                <el-button size="mini" @click="handleEdit(scope.row.userKey)"
                  >编辑</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 50]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            style="margin-top: 10px"
          ></el-pagination>
        </div>
      </el-col>
    </el-row>
    <!-- 弹出新增窗口 -->
    <el-dialog
      title="用户信息编辑"
      :visible.sync="dialogFormVisible"
      width="900px"
    >
      <el-form
        ref="pojoForm"
        :rules="rules"
        label-width="100px"
        label-position="right"
        :model="pojo"
        style="width: 850px"
        class="demo-form-inline"
        size="mini"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="用户名称" prop="userName">
              <el-input
                v-model="pojo.userName"
                @blur="handleBlur"
              ></el-input></el-form-item
          ></el-col>
          <el-col :span="8">
            <el-form-item label="简拼" prop="py">
              <el-input v-model="pojo.py"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="性别" prop="sexKey">
              <el-select v-model="pojo.sexKey" style="width: 100%">
                <el-option
                  v-for="option in sexList"
                  :key="option.code"
                  :label="option.text"
                  :value="option.code"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="登录名" prop="loginName">
              <el-input type="input" style="display: none"></el-input>
              <el-input v-model="pojo.loginName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="密码" prop="password">
              <el-input type="password" style="display: none"></el-input>
              <el-input
                v-model="pojo.password"
                type="password"
                autocomplete="new-password"
                :disabled="pojo.userKey == null ? false : true"
              ></el-input
            ></el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="出生日期" prop="birthday">
              <el-date-picker
                v-model="pojo.birthday"
                type="date"
                placeholder="选择日期"
                style="width: 100%"
                format="yyyy-MM-dd"
                value-format="yyyyMMddHHmmss"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8"
            ><el-form-item label="身份证号" prop="idNumber">
              <el-input v-model="pojo.idNumber"></el-input> </el-form-item
          ></el-col>
          <el-col :span="8"
            ><el-form-item label="电话号码" prop="phoneNumber">
              <el-input v-model="pojo.phoneNumber"></el-input> </el-form-item
          ></el-col>
          <el-col :span="8"
            ><el-form-item label="民族" prop="nation">
              <el-select v-model="pojo.nation" style="width: 100%">
                <el-option
                  v-for="option in nationList"
                  :key="option.code"
                  :label="option.text"
                  :value="option.code"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="8"
            ><el-form-item label="学历" prop="education">
              <el-select v-model="pojo.education" style="width: 100%">
                <el-option
                  v-for="option in educationList"
                  :key="option.code"
                  :label="option.text"
                  :value="option.code"
                ></el-option> </el-select></el-form-item
          ></el-col>
          <el-col :span="8"
            ><el-form-item label="职称" prop="titleKey">
              <el-select v-model="pojo.titleKey" style="width: 100%">
                <el-option
                  v-for="option in titleKeyList"
                  :key="option.code"
                  :label="option.text"
                  :value="option.code"
                ></el-option> </el-select></el-form-item
          ></el-col>
          <el-col :span="8"
            ><el-form-item label="职务" prop="positionKey">
              <el-select v-model="pojo.positionKey" style="width: 100%">
                <el-option
                  v-for="option in positionKeyList"
                  :key="option.positionKey"
                  :label="option.positionName"
                  :value="option.positionKey"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8"
            ><el-form-item label="头像" prop="avatar">
              <el-input v-model="pojo.avatar"></el-input> </el-form-item
          ></el-col>
          <el-col :span="16">
            <el-form-item label="邮箱" prop="eMail">
              <el-input v-model="pojo.eMail"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8"
            ><el-form-item label="医师证书号" prop="doctorServiceNo">
              <el-input
                v-model="pojo.doctorServiceNo"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="8"
            ><el-form-item label="医师编码" prop="doctorCertificateNo">
              <el-input
                v-model="pojo.doctorCertificateNo"
              ></el-input></el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="8"
            ><el-form-item label="默认科室" prop="defaultDeptKey">
              <el-select v-model="pojo.defaultDeptKey" style="width: 100%">
                <el-option
                  v-for="option in deptList"
                  :key="option.deptKey"
                  :label="option.deptName"
                  :value="option.deptKey"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="其他科室" prop="depts">
              <el-select
                v-model="pojo.depts"
                multiple
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="option in deptList"
                  :key="option.deptKey"
                  :label="option.deptName"
                  :value="option.deptKey"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="用户角色" prop="roles">
              <el-select
                v-model="pojo.roles"
                multiple
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="option in roleList"
                  :key="option.roleKey"
                  :label="option.roleName"
                  :value="option.roleKey"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24"
            ><el-form-item label="用户工种" prop="works">
              <el-select
                v-model="pojo.works"
                multiple
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="option in workList"
                  :key="option.code"
                  :label="option.text"
                  :value="option.code"
                >
                </el-option></el-select></el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="24"
            ><el-form-item label="备注" prop="introduction">
              <el-input
                type="textarea"
                v-model="pojo.introduction"
              ></el-input> </el-form-item
          ></el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" size="mini"
          >取 消</el-button
        >
        <el-button
          type="primary"
          @click="!saveFlg ? addData('pojoForm') : updateData('pojoForm')"
          size="mini"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { search as orgSearch } from "@/api/sys/sysHospital";
import { selectList as dictSelect } from "@/api/sys/sysDictionary";
import { selectList as positionSelect } from "@/api/sys/sysPosition";
import { list as deptSelect } from "@/api/org/orgDept";
import { selectList as roleSelect } from "@/api/org/orgRole";
import {
  search,
  add,
  getById,
  update,
  updateStatus,
  updatePwd,
} from "@/api/sys/sysUser";
import vPinyin from "@/utils/py";

// 状态类型
const statusOptions = [
  { type: 0, name: "停用" },
  { type: 1, name: "启用" },
];

export default {
  name: "sysUser",
  data() {
    return {
      orgList: [],
      orgTotal: 0, // 总记录数
      orgCurrentPage: 1, // 当前页, 默认第1页
      orgPageSize: 10, // 每页显示条数， 10条
      orgMap: {
        parentOrgKey: "ROOT",
        orgName: "",
        activeFlg: "1",
      }, // 条件查询绑定条件值
      list: [],
      sexList: [], // 性别下拉框
      nationList: [], // 民族下拉框
      educationList: [], // 学历下拉框
      titleKeyList: [], // 职称下拉框
      positionKeyList: [], // 职务下拉框
      deptMap: {
        orgKey: null,
        activeFlg: "1",
      }, // 机构科室查询
      deptList: [], // 机构科室下拉框
      roleMap: {
        orgKey: null,
      }, // 机构角色查询
      roleList: [], // 机构角色下拉框
      workList: [], // 用户工种下拉框
      total: 0, // 总记录数
      currentPage: 1, // 当前页, 默认第1页
      pageSize: 10, // 每页显示条数， 10条
      userMap: {
        orgKey: null,
        userName: "",
        activeFlg: "1",
      }, // 条件查询绑定条件值
      statusOptions: statusOptions, // 状态类型
      saveFlg: false, // 新增/更新状态 false: 新增； true：更新
      dialogFormVisible: false, // 控制对话框
      pojo: {
        userKey: null,
        orgKey: null,
        loginName: "",
        password: "",
        userName: "",
        py: "",
        sexKey: "",
        birthday: "",
        idNumber: "",
        phoneNumber: "",
        nation: "",
        education: "",
        titleKey: "",
        positionKey: "",
        defaultDeptKey: "",
        eMail: "",
        introduction: "",
        doctorServiceNo: "",
        doctorCertificateNo: "",
        avatar: "",
        userPic: "",
        works: "",
        depts: "",
        roles: "",
      }, // 提交的数据
      rules: {
        // 校验规则
        userName: [
          {
            required: true,
            message: "用户名称不能为空",
            trigger: "blur",
          },
        ],
        loginName: [
          {
            required: true,
            message: "登录名不能为空",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "密码不能为空",
            trigger: "blur",
          },
        ],
        sexKey: [
          {
            required: true,
            message: "性别不能为空",
            trigger: "blur",
          },
        ],
        defaultDeptKey: [
          {
            required: true,
            message: "默认科室不能为空",
            trigger: "blur",
          },
        ],
        roles: [
          {
            required: true,
            message: "角色不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.fetchData();
    this.dataSelect();
  },
  methods: {
    // 查询功能
    fetchData() {
      orgSearch(this.orgCurrentPage, this.orgPageSize, this.orgMap).then(
        (response) => {
          const reqs = response;
          this.orgTotal = reqs.data.total;
          this.orgList = reqs.data.rows;
        }
      );
    },
    // 字典数据列表
    dataSelect() {
      // 性别
      dictSelect("Sex").then((response) => {
        const resp = response;
        if (resp.flag) {
          this.sexList = response.data;
        } else {
          // 查询失败
          this.$message({ message: resp.message, type: "warning" });
        }
      });
      // 民族
      dictSelect("Nation").then((response) => {
        const resp = response;
        if (resp.flag) {
          this.nationList = response.data;
        } else {
          // 查询失败
          this.$message({ message: resp.message, type: "warning" });
        }
      });
      // 学历
      dictSelect("Education").then((response) => {
        const resp = response;
        if (resp.flag) {
          this.educationList = response.data;
        } else {
          // 查询失败
          this.$message({ message: resp.message, type: "warning" });
        }
      });
      // 职称
      dictSelect("TechnicalTitle").then((response) => {
        const resp = response;
        if (resp.flag) {
          this.titleKeyList = response.data;
        } else {
          // 查询失败
          this.$message({ message: resp.message, type: "warning" });
        }
      });
      // 职务
      positionSelect().then((response) => {
        const resp = response;
        if (resp.flag) {
          this.positionKeyList = response.data;
        } else {
          // 查询失败
          this.$message({ message: resp.message, type: "warning" });
        }
      });
      // 工种
      dictSelect("TypeOfWork").then((response) => {
        const resp = response;
        if (resp.flag) {
          this.workList = response.data;
        } else {
          // 查询失败
          this.$message({ message: resp.message, type: "warning" });
        }
      });
    },
    // 当每页条数改变后触发
    orgHandleSizeChange(val) {
      this.orgPageSize = val;
      this.fetchData();
    },
    // 当页码改变后触发
    orgHandleCurrentChange(val) {
      this.orgCurrentPage = val;
      this.fetchData();
    },
    // 机构选中行触发
    changOrgRows(val) {
      if (val != null) {
        this.userMap.orgKey = val.orgKey;
        this.pojo.orgKey = val.orgKey;
        this.deptMap.orgKey = val.orgKey;
        this.roleMap.orgKey = val.orgKey;
        // 查询机构用户
        this.selectUserData();
        // 机构科室列表
        this.selectDeptList();
        // 机构角色列表
        this.selectRoleList();
      } else {
        this.userMap.orgKey = null;
        this.pojo.orgKey = null;
        this.deptMap.orgKey = null;
        this.roleMap.orgKey = null;
        this.total = null;
        this.list = null;
      }
    },
    // 启用/停用按钮
    handleIsDisplay(id, status) {
      this.$confirm(
        status === 1 ? "确认停用这条记录吗?" : "确认启用这条记录吗?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          // 确认
          updateStatus(id, status).then((response) => {
            const resp = response;
            if (resp.flag) {
              // 更新成功, 刷新列表数据
              this.selectUserData();
              this.$message({ message: resp.message, type: "success" });
            } else {
              // 更新失败
              this.$message({ message: resp.message, type: "warning" });
            }
          });
        })
        .catch(() => {
          // 取消
        });
    },
    // 查询机构用户
    selectUserData() {
      if (this.userMap.orgKey == null) {
        // 查询失败
        this.$message({ message: "请选择机构信息!", type: "warning" });
      } else {
        search(this.currentPage, this.pageSize, this.userMap).then(
          (response) => {
            const reqs = response;
            this.total = reqs.data.total;
            this.list = reqs.data.rows;
          }
        );
      }
    },
    // 机构科室列表
    selectDeptList() {
      deptSelect(this.deptMap).then((response) => {
        const reqs = response;
        this.deptList = reqs.data;
      });
    },
    // 机构角色列表
    selectRoleList() {
      roleSelect(this.roleMap).then((response) => {
        const reqs = response;
        this.roleList = reqs.data;
      });
    },
    // 新增机构用户
    handleAdd() {
      if (this.userMap.orgKey == null) {
        // 查询失败
        this.$message({ message: "请选择机构信息!", type: "warning" });
      } else {
        this.dialogFormVisible = true;
        this.saveFlg = false;
        this.pojo.userKey = null;
        this.$nextTick(() => {
          // 异步事件
          this.$refs["pojoForm"].resetFields();
        });
      }
    },
    // 生成拼音
    handleBlur() {
      this.pojo.py = vPinyin.getFirstLettersUpper(this.pojo.userName);
    },
    // 新增数据
    addData(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          add(this.pojo).then((response) => {
            const resp = response;
            if (resp.flag) {
              // 新增成功, 刷新列表数据
              this.selectUserData();
              this.dialogFormVisible = false; //关闭窗口
              this.$message({ message: resp.message, type: "success" });
            } else {
              // 新增失败
              this.$message({ message: resp.message, type: "warning" });
            }
          });
        } else {
          return false;
        }
      });
    },
    // 更新数据
    updateData(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          update(this.pojo).then((response) => {
            const resp = response;
            if (resp.flag) {
              // 新增成功, 刷新列表数据
              this.selectUserData();
              this.dialogFormVisible = false; //关闭窗口
              this.$message({ message: resp.message, type: "success" });
            } else {
              this.$message({ message: resp.message, type: "warning" });
              // 更新失败
            }
          });
        } else {
          return false;
        }
      });
    },
    // 打开编辑窗口
    handleEdit(id) {
      this.handleAdd();
      this.saveFlg = true;
      getById(id).then((response) => {
        const resp = response;
        if (resp.flag) {
          this.pojo = resp.data;
        } else {
          this.$message({ message: resp.message, type: "warning" });
        }
      });
    },
    // 密码重置(设置初始化密码)
    handlePassword(id) {
      this.$confirm("确认是否重置用户密码，初始化密码为: 123456", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 确认
          updatePwd(id, "123456").then((response) => {
            const resp = response;
            if (resp.flag) {
              // 更新成功, 刷新列表数据
              this.selectUserData();
              this.$message({ message: resp.message, type: "success" });
            } else {
              // 更新失败
              this.$message({ message: resp.message, type: "warning" });
            }
          });
        })
        .catch(() => {
          // 取消
        });
    },
    // 重置功能
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 当每页条数改变后触发
    handleSizeChange(val) {
      this.pageSize = val;
      this.selectUserData();
    },
    // 当页码改变后触发
    handleCurrentChange(val) {
      this.currentPage = val;
      this.selectUserData();
    },
    // 切换状态按钮
    chageStatus() {
      this.selectUserData();
    },
  },
  filters: {
    statusFilter(type) {
      // 全局的 statusOptions , 返回一个满足条件的对象
      const obj = statusOptions.find((obj) => obj.type === type);
      // 非空返回类型名称
      return obj ? obj.name : null;
    },
  },
};
</script>

<style>
</style>